﻿using Kimd.Logic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;
using WorkStation.Logic;
using WorkStation.UI.Controls;

namespace WorkStation.UI
{
    public partial class FormRecipe : Form, IUserChange
    {
        private FormMain _mainForm;
        public FormRecipe(FormMain mainForm)
        {
            InitializeComponent();
            this._mainForm = mainForm;
        }

        private void FormRecipe_Load(object sender, EventArgs e)
        {
            this.pG_Mes.SelectedObject = SystemConfig.Instance.MesParam;
            this.pG_Tester.SelectedObject = SystemConfig.Instance.TestParam;
            this.rB_Arm1FeedTester.Checked = true;

            this.pG_InUse.SelectedObject = SystemConfig.Instance.UsingStatus;

            this.pG_SpotCheckSN.SelectedObject = SystemConfig.Instance.SpotSNs;

            if (SystemConfig.Instance.ArmGetTesterParam == null || SystemConfig.Instance.ArmGetTesterParam.Count == 0)
                return;
            pG_Get.SelectedObject = SystemConfig.Instance.ArmGetTesterParam[0];
            pG_Put.SelectedObject = SystemConfig.Instance.ArmPutTesterParam[0];
        }

        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            SystemConfig.Instance.Save();
        }
        public void UserChanged()
        {
            if (GlobalVariable.UserRole.UserLevel == 2)
            {
                this.pG_Mes.Enabled = true;
                this.pG_Tester.Enabled = true;
                this.pG_Get.Enabled = true;
                this.pG_Put.Enabled = true;
            }
            else if (GlobalVariable.UserRole.UserLevel == 0)
            {
                this.pG_Mes.Enabled = false;
                this.pG_Tester.Enabled = false;
                this.pG_Get.Enabled = false;
                this.pG_Put.Enabled = false;
            }
            else if (GlobalVariable.UserRole.UserLevel == 1)
            {
                this.pG_Mes.Enabled = true;
                this.pG_Tester.Enabled = true;
                this.pG_Get.Enabled = true;
                this.pG_Put.Enabled = true;
            }
        }

        private void rB_Feed_Click(object sender, EventArgs e)
        {
            
        }

        private void rB_Feed_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked)
            {
                if (rb.Name.Equals("rB_Arm1FeedTester"))
                {
                    pG_Get.SelectedObject = SystemConfig.Instance.ArmGetTesterParam[0];
                    pG_Put.SelectedObject = SystemConfig.Instance.ArmPutTesterParam[0];
                }
                else if (rb.Name.Equals("rB_Arm2FeedTester"))
                {
                    pG_Get.SelectedObject = SystemConfig.Instance.ArmGetTesterParam[1];
                    pG_Put.SelectedObject = SystemConfig.Instance.ArmPutTesterParam[1];
                }
                else if (rb.Name.Equals("rB_Arm3FeedTester"))
                {
                    pG_Get.SelectedObject = SystemConfig.Instance.ArmGetTesterParam[2];
                    pG_Put.SelectedObject = SystemConfig.Instance.ArmPutTesterParam[2];
                }
                else if (rb.Name.Equals("rB_Arm4FeedTester"))
                {
                    pG_Get.SelectedObject = SystemConfig.Instance.ArmGetTesterParam[3];
                    pG_Put.SelectedObject = SystemConfig.Instance.ArmPutTesterParam[3];
                }
                else if (rb.Name.Equals("rB_Arm1FeedPrecision"))
                {
                    pG_Get.SelectedObject = SystemConfig.Instance.ArmGetPrecisionParam[0];
                    pG_Put.SelectedObject = SystemConfig.Instance.ArmPutPrecisionParam[0];
                }
                else if (rb.Name.Equals("rB_Arm2FeedPrecision"))
                {
                    pG_Get.SelectedObject = SystemConfig.Instance.ArmGetPrecisionParam[1];
                    pG_Put.SelectedObject = SystemConfig.Instance.ArmPutPrecisionParam[1];
                }
                else if (rb.Name.Equals("rB_Arm3FeedPrecision"))
                {
                    pG_Get.SelectedObject = SystemConfig.Instance.ArmGetPrecisionParam[2];
                    pG_Put.SelectedObject = SystemConfig.Instance.ArmPutPrecisionParam[2];
                }
                else if (rb.Name.Equals("rB_Arm4FeedPrecision"))
                {
                    pG_Get.SelectedObject = SystemConfig.Instance.ArmGetPrecisionParam[3];
                    pG_Put.SelectedObject = SystemConfig.Instance.ArmPutPrecisionParam[3];
                }
            }
        }

        private void pG_InUse_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //SystemConfig.Instance.Save();
            
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            //GlobalVariable.CaptureTrayFront.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsFrontDisable;

            //GlobalVariable.CaptureTrayBack.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsBackDisable;

            //GlobalVariable.PrecisionFront.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsFrontDisable;

            //GlobalVariable.PrecisionBack.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsBackDisable;

            //GlobalVariable.PutOKNGFront.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsFrontDisable;

            //GlobalVariable.putScanNGLogicFront.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsFrontDisable;

            //GlobalVariable.PutOKNGBack.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsBackDisable;

            //GlobalVariable.putScanNGLogicBack.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsBackDisable;

            //GlobalVariable.RecycleTrayFront.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsFrontDisable;

            //GlobalVariable.RecycleTrayBack.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsBackDisable;

            ////GlobalVariable.RobotStatusLogic1.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsRobot1Disable;

            ////GlobalVariable.RobotStatusLogic2.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsRobot2Disable;

            //GlobalVariable.RobotLogic1.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsRobot1Disable;

            //GlobalVariable.RobotLogic2.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsRobot2Disable;

            //for (int i = 0; i < 12; i++)
            //{
            //    if (i < 6)
            //    {
            //        GlobalVariable.TestLogics[i].Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsFrontDisable;
            //        //FrontTesters.Add(testLogic);
            //    }
            //    else
            //    {
            //        GlobalVariable.TestLogics[i].Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsBackDisable;
            //        //BackTesters.Add(testLogic);
            //    }

            //}

            //for (int i = 0; i < 4; i++)
            //{
            //    if (i < 2)
            //    {
            //        GlobalVariable.ArmLogics[i].Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsFrontDisable;
            //    }
            //    else
            //        GlobalVariable.ArmLogics[i].Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsBackDisable;

            //}

            //GlobalVariable.clearLogicFront.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsFrontDisable;

            //GlobalVariable.clearLogicBack.Status.IsEnabled = !SystemConfig.Instance.UsingStatus.IsBackDisable;

            //GlobalVariable.IsCosmeticMode = SystemConfig.Instance.UsingStatus.IsCosmetic;

            SystemConfig.Instance.Save();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            SystemConfig.Instance.Save();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void btn_CheckIn_Click(object sender, EventArgs e)
        {
            string eventId = ((Button)sender).Name;
            switch(eventId)
            {
                case "btn_CheckIn":
                    if(!CommonMethod.DoMesCheckIn("123", out string errInfo))
                    {
                        MessageBox.Show(errInfo);
                    }
                    break;
                case "btn_FeedingCheck":
                    if (!CommonMethod.DoFeedingCheck("123", out string errInfo2))
                    {
                        MessageBox.Show(errInfo2);
                    }
                    break;
                case "btn_CarrierBind":
                    if (!CommonMethod.DoCarrierBind("123", "1", "ABC", out string errInfo3))
                    {
                        MessageBox.Show(errInfo3);
                    }
                    break;
                case "btn_CarrierCheck":
                    string sn = tb_carrierSN.Text.Trim();
                    if (string.IsNullOrEmpty(sn))
                    {
                        MessageBox.Show("条码为空!");
                        return;
                    }
                    if (!CommonMethod.DoCarrierCheck($"{sn}", out string errInfo444))
                    {
                        MessageBox.Show(errInfo444);
                        break;
                    }
                    MessageBox.Show($"{sn},OK");
                    break;
            }
        }

        public void UpdateMesParam()
        {
            this.pG_Mes.SelectedObject = SystemConfig.Instance.MesParam;
            this.pG_Mes.Invalidate();
            this.pG_Mes.Refresh();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            tb_FrontSN.Text = "";
            tb_FrontSN.Text = GlobalVariable.CurrentFrontOKBoatSN;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            tb_BackSn.Text = "";
            tb_BackSn.Text = GlobalVariable.CurrentBackOKBoatSN;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sn = tb_FrontSN.Text.Trim();
            if (string.IsNullOrEmpty(sn))
            {
                MessageBox.Show("条码为空!");
                return;
            }
            if (MessageBox.Show($"将设定前站的下料Boat的SN由 {GlobalVariable.CurrentFrontOKBoatSN} 变为 {sn}","Tip",
                MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
            {
                GlobalVariable.CurrentFrontOKBoatSN = sn;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string sn = tb_BackSn.Text.Trim();
            if (string.IsNullOrEmpty(sn))
            {
                MessageBox.Show("条码为空!");
                return;
            }
            if (MessageBox.Show($"将设定后站的下料Boat的SN由 {GlobalVariable.CurrentBackOKBoatSN} 变为 {sn}", "Tip",
                MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                GlobalVariable.CurrentBackOKBoatSN = sn;
            }
        }
    }
}
